package com.cloudkinto.common.utils;

import com.cloudkinto.common.common.DateUtils;
import org.apache.commons.lang3.StringUtils;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Author Administrator
 *
 * @Des
 * @Version 2019/6/3
 */
public class TimeUtils {
    public static String YYYYMMDD = "yyyy-MM-dd";
    public static final String YYYYMMDDSLASH = "yyyy/MM/dd";
    public static final String YYYYMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static String YYYYMMDDChinese = "yyyy年MM月dd日";
    public static String MMDD = "MM-dd";
    public static String MMDDSLASH = "MM/dd";
    public static String MMDDChinese = "MM月dd日";

    public static Date getDateByTimezone(Date date, String timezone) {
        return getDateByTimezone(date, YYYYMMddHHmmss, timezone);
    }

    public static Date getDateByTimezone(Date date, String formatStr, String timezone) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
        dateFormat.setTimeZone(TimeZone.getTimeZone(timezone)); // 设置时区为GMT
        String format = dateFormat.format(date);
        return TimeUtils.parseDate(format, formatStr);
    }

    /**
     * @return yyyy-MM-dd
     */
    public static String getCurrentDateFormat() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(new Date());
    }

    /**
     * @return format
     */
    public static String getCurrentDateFormat(String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(new Date());
    }

    /**
     * @return format
     */
    public static String getAfterTimeFormat(String format, int hours, int mins) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, hours);
        calendar.add(Calendar.MINUTE, mins);
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * @return format
     */
    public static String getAfterTimeFormat(String date, String format, int hours, int mins) {
        Date date1 = TimeUtils.parseDate(date, format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        calendar.add(Calendar.HOUR, hours);
        calendar.add(Calendar.MINUTE, mins);
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * @return yyyy-MM-dd
     */
    public static String getCurrentDateFormat(int addDays) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, addDays);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(calendar.getTime());
    }


    public static String getMMddDate(int addDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, addDay);
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        return dateFormat.format(calendar.getTime());
    }

    public static String getddDate(int addDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, addDay);
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd");
        return dateFormat.format(calendar.getTime());
    }

    public static String getYYYYMMDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
        return dateFormat.format(new Date());
    }

    public static String getYYYYMMDate(int addMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, addMonth);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
        return dateFormat.format(new Date());
    }

    public static String getYYYYMMDate(int addMonth, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, addMonth);
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(new Date());
    }

    public static String getCurrentDateTimeFormat() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(new Date());
    }

    /**
     * "yyyy-MM-dd" 格式
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        if (date == null) return "";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

    /**
     * "yyyy-MM-dd" 格式
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date, String format) {
        if (date == null) return "";
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    /**
     * "yyyy-MM-dd"
     *
     * @param date
     * @return
     */
    public static Date parseDate(String date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
    }

    /**
     * "yyyy-MM-dd"
     *
     * @param date
     * @return
     */
    public static Date parseDate(String date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            return dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
    }


    /**
     * "yyyy-MM-dd" 格式
     *
     * @param date
     * @return
     */
    public static Date formatDateToDate(Date date, String format, String timeZone) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone)); // 设置时区为GMT
        return DateUtils.stringFormatToDate(dateFormat.format(date), format);
    }

    /**
     * 把时间转换时区
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date, String format, String timeZone) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone)); // 设置时区为GMT
        return dateFormat.format(date);
    }

    public static String formatTime(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
        return dateFormat.format(date);
    }

    /**
     * 多格式格式化字符串日期
     * <p>
     * YYYYMMDDSLASH = "yyyy/MM/dd";
     * YYYYMMDDChinese = "yyyy年MM月dd日";
     * MMDD = "MM-dd";
     * MMDDSLASH = "MM/dd";
     * MMDDChinese = "MM月dd日";
     *
     * @param dateStr
     * @return
     */
    public static String parseDateMuti(String dateStr) {
        SimpleDateFormat dateFormat1 = new SimpleDateFormat(MMDDSLASH);
        SimpleDateFormat dateFormat2 = new SimpleDateFormat(MMDDChinese);
        SimpleDateFormat dateFormat3 = new SimpleDateFormat(MMDD);
        SimpleDateFormat dateFormat4 = new SimpleDateFormat(YYYYMMDDSLASH);
        SimpleDateFormat dateFormat5 = new SimpleDateFormat(YYYYMMDDChinese);
        Date date = new Date();
        try {
            date = dateFormat1.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            try {
                date = dateFormat2.parse(dateStr);
            } catch (ParseException ex) {
                ex.printStackTrace();
                try {
                    date = dateFormat3.parse(dateStr);
                } catch (ParseException exc) {
                    exc.printStackTrace();
                    try {
                        date = dateFormat4.parse(dateStr);
                    } catch (ParseException e1) {
                        e1.printStackTrace();
                        try {
                            date = dateFormat5.parse(dateStr);
                        } catch (ParseException e2) {
                            e2.printStackTrace();
                        }
                    }
                }
            }
        }
        return formatDate(date);
    }

    public static Date getDateAdd(Date date, int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, i);
        return calendar.getTime();
    }

    public static String formatDateMMdd(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        return dateFormat.format(date);
    }

    /**
     * 获取上个月的日期
     *
     * @return
     */
    public static String getLastMonthDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        return dateFormat.format(calendar.getTime());
    }

    public static int getBetweenDays(Date parseDate) {
        long timeBetweenMillins = System.currentTimeMillis() - parseDate.getTime();
        return (int) timeBetweenMillins / 1000 / 60 / 60 / 24;
    }

    /**
     * 2007-10-23T17:15:44.000Z 根据这个字符串放回Date类型的时间
     *
     * @param time
     * @return
     */
    public static Date getDateTime(String time) {
        try {
            time = time.replace("Z", " UTC");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dt = sdf.parse(time);
            return dt;
        } catch (Exception e) {
            return new Date();
        }
    }

    /**
     * 2007-10-23T17:15:44Z 根据这个字符串放回Date类型的时间
     *
     * @param time
     * @return
     */
    public static Date getDateTime2(String time) {
        try {
            time = time.replace("Z", " UTC");
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z");
            Date dt = sdfDate.parse(time);
            return dt;
        } catch (Exception e) {
            return new Date();
        }
    }

    /**
     * 2007-10-23T17:15:44Z 根据这个字符串返回Date类型的时间
     *
     * @param time
     * @return
     */
    public static Date getDateTime2(String time, String timeZone) {
        try {
            time = time.replace("Z", " UTC");
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z");
            Date dt = sdfDate.parse(time);
            SimpleDateFormat localSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            localSdf.setTimeZone(TimeZone.getTimeZone(timeZone));
            String format = localSdf.format(dt);
            return TimeUtils.parseDate(format, "yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
            return new Date();
        }
    }

    public static String addDaysFormat(String dateStr, String format, int days) {
        Date date = TimeUtils.parseDate(dateStr, format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return TimeUtils.formatDate(calendar.getTime(), format);
    }


    public static int getWeekOfDate(Date dt) {
        int[] weekDays = {7, 1, 2, 3, 4, 5, 6};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static String getYamatoDTime(String distributionTime) {
        if (StringUtils.isBlank(distributionTime) || "0".equals(distributionTime)) {
            return ("");//配达日时间带
        } else if (!distributionTime.contains("-")) {
            return distributionTime;
        } else if (distributionTime.contains("午前中")) {
            return "0812";
        } else if (distributionTime.equals("-")) {
            return "";
        } else if (distributionTime.equals("08-12") || distributionTime.equals("16-18") || distributionTime.equals("14-16")
                || distributionTime.equals("18-20") || distributionTime.equals("19-21") || distributionTime.equals("19-")) {
            return distributionTime.replace("-", "");
        } else {
            String regEx = "[^0-9||^０-９||-]";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(distributionTime);
            distributionTime = m.replaceAll("").trim();
            int first = 0;
            int last = 0;
            try {
                first = Integer.parseInt(distributionTime.split("-")[0]);
                last = Integer.parseInt(distributionTime.split("-")[1]);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }  catch (ArrayIndexOutOfBoundsException e) {
                e.printStackTrace();
            }
            if (last <= 12) {
                return ("0812");
            } else if (last <= 16) {
                if (first <= 9) {
                    return ("0812");
                } else if (first <= 14) {
                    return ("1416");
                } else {
                    return ("1416");
                }
            } else if (last <= 18) {
                if (first <= 9) {
                    return ("0812");
                } else if (first <= 14) {
                    return ("1416");
                } else {
                    return ("1618");
                }
            } else if (last <= 20) {
                if (first <= 9) {
                    return ("0812");
                } else if (first <= 14) {
                    return ("1618");
                } else {
                    return ("1820");
                }
            } else {
                if (first <= 9) {
                    return ("0812");
                } else if (first <= 18) {
                    return ("1820");
                } else {
                    return ("1921");
                }
            }
        }
    }

    public static String getSagawaDTime(String deliveryTime) {
        try {
            if (StringUtils.isNotBlank(deliveryTime)) {
                if (deliveryTime.contains("午前中"))
                    return "01";
                if (!deliveryTime.contains("-"))
                    return deliveryTime;
                int first = Integer.parseInt(deliveryTime.split("-")[0]);
                int last = Integer.parseInt(deliveryTime.split("-")[1]);
                if (last <= 12) {
                    return ("01");
                } else if (last <= 14) {
                    if (first <= 9) {
                        return ("01");
                    } else {
                        return ("12");
                    }
                } else if (last <= 14) {
                    if (first <= 9) {
                        return ("01");
                    } else if (first <= 12) {
                        return ("12");
                    } else {
                        return ("14");
                    }
                } else if (last <= 16) {
                    if (first <= 9) {
                        return ("01");
                    } else if (first <= 12) {
                        return ("12");
                    } else if (first <= 14) {
                        return ("14");
                    } else {
                        return ("16");
                    }
                } else if (last <= 18) {
                    if (first <= 9) {
                        return ("01");
                    } else if (first <= 12) {
                        return ("12");
                    } else if (first <= 14) {
                        return ("14");
                    } else {
                        return ("16");
                    }
                } else if (last <= 20) {
                    if (first <= 9) {
                        return ("01");
                    } else if (first <= 12) {
                        return ("12");
                    } else if (first <= 14) {
                        return ("14");
                    } else if (first <= 16) {
                        return ("16");
                    } else {
                        return ("04");
                    }
                } else {
                    if (first <= 9) {
                        return ("01");
                    } else if (first <= 12) {
                        return ("12");
                    } else if (first <= 14) {
                        return ("14");
                    } else if (first <= 16) {
                        return ("16");
                    } else if (first <= 18) {
                        return ("04");
                    } else if (first <= 19) {
                        return ("04");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "0";
    }

    public static String getSagawaDTimeNew(String deliveryTime) {
        try {
            if (StringUtils.isNotBlank(deliveryTime)) {
                if (!deliveryTime.contains("-"))
                    return "";
                if (deliveryTime.contains("午前中"))
                    return "01";
                int first = Integer.parseInt(deliveryTime.split("-")[0]);
                int last = Integer.parseInt(deliveryTime.split("-")[1]);
                if (last <= 12) {
                    return ("01");
                } else if (last <= 14) {
                    if (first <= 9) {
                        return ("01");
                    } else {
                        return ("12");
                    }
                } else if (last <= 14) {
                    if (first <= 9) {
                        return ("01");
                    } else if (first <= 12) {
                        return ("12");
                    } else {
                        return ("14");
                    }
                } else if (last <= 16) {
                    if (first <= 9) {
                        return ("01");
                    } else if (first <= 12) {
                        return ("12");
                    } else if (first <= 14) {
                        return ("14");
                    } else {
                        return ("16");
                    }
                } else if (last <= 18) {
                    if (first <= 9) {
                        return ("01");
                    } else if (first <= 12) {
                        return ("12");
                    } else if (first <= 14) {
                        return ("14");
                    } else {
                        return ("16");
                    }
                } else {
                    if (first <= 9) {
                        return ("01");
                    } else if (first <= 12) {
                        return ("12");
                    } else if (first <= 14) {
                        return ("14");
                    } else if (first <= 16) {
                        return ("16");
                    } else {
                        return ("04");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getXinongDTime(String deliveryTime) {
        try {
            if (StringUtils.isNotBlank(deliveryTime)) {
                if (!deliveryTime.contains("-"))
                    return deliveryTime;
                int first = Integer.parseInt(deliveryTime.split("-")[0]);
                int last = Integer.parseInt(deliveryTime.split("-")[1]);
                if (last <= 12) {
                    return ("1");
                } else {
                    return ("2");
                }
            }
        } catch (Exception ee) {
            ee.printStackTrace();
        }
        return "0";
    }

    public static String getTimeAdd(int hours, int min, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY, hours);
        calendar.add(Calendar.MINUTE, min);
        return TimeUtils.formatDate(calendar.getTime(), format);
    }
}
